package com.jsmile.cloud.msgcenter.pay.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.egzosn.pay.web.support.HttpRequestNoticeParams;
import com.jsmile.cloud.msgcenter.api.req.ReqPayNotify;
import com.jsmile.cloud.msgcenter.web.config.PayConfig;
import com.jsmile.cloud.msgcenter.zf.req.ali.AliNotifyResponse;
import com.jsmile.cloud.msgcenter.zf.strategy.factory.AliNotifyFactory;
import com.jsmile.mall.api.constants.JSmileConstant;
import com.jsmile.mall.api.enums.LogTypeEnum;
import com.jsmile.mall.log.annotation.JsmileLog;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 支付回调控制器
 * @date 2018年08月25日 04:57:24
 */
@RestController
@RequestMapping("/api")
@Api(tags = "微信支付回调")
@Slf4j
@AllArgsConstructor
public class PayNotifyController {

    private final AliNotifyFactory aliNotifyFactory;

    @RequestMapping(value = "/pay/notify")
    @JsmileLog(title = "消息中心->微信和支付宝支付回调通知处理", type = LogTypeEnum.QUERY, systemId = JSmileConstant.SystemId.MSG_CENTER)
    public String payBack(HttpServletRequest request, ReqPayNotify reqPayNotify) {
        return PayConfig.getPayService(reqPayNotify.getThirdAppId(), reqPayNotify.getThirdPayType()).payBack(new HttpRequestNoticeParams(request)).toMessage();
    }

    @RequestMapping(value = "/ali/callback/notify")
    @JsmileLog(title = "消息中心->支付宝网关回调处理", type = LogTypeEnum.QUERY, systemId = JSmileConstant.SystemId.MSG_CENTER)
    public String payBack(Map<String, Object> map) {
        String request = JSON.toJSONString(map);
        AliNotifyResponse response = JSON.parseObject(request, AliNotifyResponse.class);
        return aliNotifyFactory.findStrategy(StrUtil.isNotBlank(response.getNotifyType()) ? response.getNotifyType() : response.getMsgMethod()).excute(request);
    }
}
